package cn.elwy.rcp.ui.explorer.core.handler;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.elwy.rcp.core.model.ui.progress.ProgressMonitor;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;

@SuppressWarnings("unchecked")
public abstract class ProjectServiceHandler {

	protected List<ProjectServiceHandler> serviceList = new ArrayList<ProjectServiceHandler>();
	protected Logger logger = LoggerFactory.getLogger(getClass());
	protected boolean isContinue = true;

	public void register(ProjectServiceHandler handler) {
		serviceList.add(handler);
	}

	public void unregister(ProjectServiceHandler handler) {
		serviceList.remove(handler);
	}

	protected abstract Object handler(ProjectConfig projectConfig, ProgressMonitor monitor);

	public final <T> T execute(ProjectConfig projectConfig, ProgressMonitor monitor) {
		Object result = handler(projectConfig, monitor);
		if (!isContinue) {
			return (T) result;
		}
		for (ProjectServiceHandler handler : serviceList) {
			result = handler.execute(projectConfig, monitor);
		}
		return (T) result;
	}

}
